package org.jctc.els.common.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

import java.util.Date;

/**
 * 日期相关的函数
 *
 * @author Rxy
 * @date 2021/4/12 16:10
 */
public class JtcDateUtil {

    public static String getBeforeTime(Date beforeDate) {
        String beforeTime = "";
        long year = DateUtil.betweenYear(DateUtil.date(beforeDate), DateUtil.date(),false);
        if(year> 0){
           return String.format("%d年之前",year);
        }
        long month = DateUtil.betweenMonth(DateUtil.date(beforeDate), DateUtil.date(),false);
        if(month > 0){
            return String.format("%d月之前",month);
        }
        long day = DateUtil.betweenDay(DateUtil.date(beforeDate), DateUtil.date(),false);
        if(day > 0){
            return String.format("%d天之前",day);
        }
        long second = DateUtil.between(DateUtil.date(beforeDate), DateUtil.date(),DateUnit.SECOND);
        if(second < 60){
            return String.format("%d秒之前",second);
        }

        long minute = DateUtil.between(DateUtil.date(beforeDate), DateUtil.date(),DateUnit.MINUTE);
        if(minute < 60){
            return String.format("%d分钟之前",minute);
        }

        long hour = DateUtil.between(DateUtil.date(beforeDate), DateUtil.date(),DateUnit.HOUR);
        if(hour < 24){
            return String.format("%d小时之前",hour);
        }

        return beforeTime;
    }

    public static String convertWeekday(int weekday){
        String weekDayStr = "";
        switch (weekday){
            case 1:
                weekDayStr = "周一";
                break;
            case 2:
                weekDayStr = "周二";
                break;
            case 3:
                weekDayStr = "周三";
                break;
            case 4:
                weekDayStr = "周四";
                break;
            case 5:
                weekDayStr = "周五";
                break;
            case 6:
                weekDayStr = "周六";
                break;
            case 7:
                weekDayStr = "周日";
                break;
        }
        return weekDayStr;
    }

    public static void main(String[] args) {
        DateTime beforeDate = DateUtil.parseDate("1999-1-1");
        String beforeTime = getBeforeTime(beforeDate);
        System.out.println(beforeTime);

        DateTime beforeDate2 = DateUtil.parseDate("2021-3-1");
        String beforeTime2 = getBeforeTime(beforeDate2);
        System.out.println(beforeTime2);

        DateTime beforeDate3 = DateUtil.parseDate("2021-4-1");
        String beforeTime3 = getBeforeTime(beforeDate3);
        System.out.println(beforeTime3);

        DateTime beforeDate4 = DateUtil.parseDateTime("2021-4-12 12:00:00");
        String beforeTime4 = getBeforeTime(beforeDate4);
        System.out.println(beforeTime4);

        DateTime beforeDate5 = DateUtil.parseDateTime("2021-4-12 17:00:00");
        String beforeTime5 = getBeforeTime(beforeDate5);
        System.out.println(beforeTime5);

        DateTime beforeDate6 = DateUtil.parseDateTime("2021-4-12 17:27:00");
        String beforeTime6 = getBeforeTime(beforeDate6);
        System.out.println(beforeTime6);
    }
}
